package com.huanying.framework.payment;

import org.springframework.stereotype.Service;
import com.huanying.framework.BaseDao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.text.html.FormSubmitEvent.MethodType;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;
import com.huanying.framework.utils.Date;
import com.huanying.framework.user.User;
@Service
public class PaymentServiceImpl implements PaymentService {

	@Autowired
	private BaseDao dao;
	
	@Override
	public void update(Trade_Money trade_money) throws Exception {
		dao.update(trade_money);
	}

    //对交易记录表当前记录的支出记录显示
	@Override
	public Trade_Money findMoneyRecordOut(int user_id) throws Exception{ 
		Trade_Money trade_money = new Trade_Money();
		String hql = new String("from Trade_Money t where t.user.id=? and direction=1");
		List<Object> params = new ArrayList<Object>();
		params.add(user_id);
		trade_money=(Trade_Money) dao.exculeHqlGetOjbectByParams(hql, params);
		return trade_money;
	}
	//对交易记录表当前记录的收入记录显示
	@Override
	public Trade_Money findMoneyRecordIn(int user_id) throws Exception{ 
		Trade_Money trade_money = new Trade_Money();
		String hql = new String("from Trade_Money t where t.userobject.id=? and direction=2");
		List<Object> params = new ArrayList<Object>();
		params.add(user_id);
		trade_money=(Trade_Money) dao.exculeHqlGetOjbectByParams(hql, params);
		return trade_money;
	}
	//对交易记录表按时间降序全部显示
	@Override
	public PageBean searchMoneyRecords(Map<String, String> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder hql = new StringBuilder("from Trade_Money as t where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map.get("user_name") != null &&!("").equals(map.get("user_name"))) {
			hql.append(" and t.user.name like ?");
			param.add("%" + map.get("user_name") + "%");
		}
		if (map.get("userobject_name") != null &&!("").equals(map.get("userobject_name"))) {
			hql.append(" and t.userobject.name like ?");
			param.add("%" + map.get("userobject_name") + "%");
		}
		
		if (!("").equals(map.get("transactiontime")) && map.get("transactiontime") != null) {
			hql.append(" and DATE_FORMAT(t.transactiontime,'%Y-%m-%d')=?");
			param.add(map.get("transactiontime"));
		}
		if (!("").equals(map.get("create_time")) && map.get("create_time") != null) {
			hql.append(" and DATE_FORMAT(t.create_time,'%Y-%m-%d')=?");
			param.add(map.get("create_time"));
		}
		if (map.get("type") != "" && map.get("type") != null && Integer.valueOf(map.get("type"))==1) {
			hql.append(" and t.type=1");
		}
		if (map.get("type") != "" && map.get("type") != null && Integer.valueOf(map.get("type"))==2) {
			hql.append(" and t.type=2");
		}
		if (map.get("direction") != "" && map.get("direction") != null &&Integer.valueOf(map.get("direction"))==1) {
			hql.append(" and t.direction=1");
		}
		if (map.get("direction") != "" && map.get("direction") != null &&Integer.valueOf(map.get("direction"))==2) {
			hql.append(" and t.direction=2"); 
		}
		if (map.get("amount") != null &&!("").equals(map.get("amount"))) {
			Double amount = Double.valueOf(map.get("amount"));
			hql.append(" and t.amount = ?");
			param.add(amount);
		}
		if (map.get("bankType") != "" && map.get("bankType") != null && Integer.valueOf(map.get("bankType"))==1) {
			hql.append(" and t.bankType=1");
		}
		if (map.get("bankType") != "" && map.get("bankType") != null &&Integer.valueOf(map.get("bankType"))==0) {
			hql.append(" and t.bankType=0");
		}
		if (map.get("status") != "" && map.get("status") != null &&Integer.valueOf(map.get("status"))==1) {
			hql.append(" and t.status=1");
		}
		if (map.get("status") != "" && map.get("status") != null && Integer.valueOf(map.get("status"))==0) {
			hql.append(" and t.status=0");
		}
    	hql.append(" order by t.create_time desc,t.id desc");

		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}
	
	//查看id的所有支出交易记录
	@Override
	public PageBean searchMoneyRecordsQueryOut(Map<String, String> map,int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder hql = new StringBuilder("from Trade_Money as t where t.status=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("user_id") != "" && map.get("user_id") != null) {
			int user_id1=Integer.parseInt(map.get("user_id"));
			hql.append(" and t.direction=1 and t.user.id = ? or(t.direction=2 and t.userobject.id = ?)");
			param.add(user_id1);
			param.add(user_id1);
		}
		hql.append(" order by t.create_time desc");

		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}
	//查看id的所有收入交易记录
	@Override
	public PageBean searchMoneyRecordsQueryIn(Map<String, String> map,int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder hql = new StringBuilder("from Trade_Money as t where t.status=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("user_id") != "" && map.get("user_id") != null) {
			int user_id1=Integer.parseInt(map.get("user_id"));
			hql.append(" and t.direction=2 and t.user.id = ? or(t.direction=1 and t.userobject.id = ?)");
			param.add(user_id1);
			param.add(user_id1);
		}
		hql.append(" order by t.create_time desc");

		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}
}
